/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SubsistemaFinanzas;

import clienteSandraParada.Login;
import java.awt.Color;
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author hernan
 */
public class PagoBono extends javax.swing.JPanel {

    private DecimalFormat df = new DecimalFormat("###,###");
    public PagoBono() {
        initComponents();
        this.actualizarTabla();
        this.ocultarColumnaID();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        dialogoCancelarPago = new javax.swing.JDialog();
        botonSiEliminar = new javax.swing.JButton();
        botonNoEliminar = new javax.swing.JButton();
        etiquetaEliminar = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        tablaPagos = new javax.swing.JTable();
        campoBuscar = new javax.swing.JTextField();
        jPanel6 = new javax.swing.JPanel();
        etiquetaMensajeria = new javax.swing.JLabel();
        jPanel7 = new javax.swing.JPanel();
        botonVer = new javax.swing.JButton();
        botonModificar = new javax.swing.JButton();
        botonEliminar = new javax.swing.JButton();

        dialogoCancelarPago.setTitle("Eliminar pago con bono");
        dialogoCancelarPago.setMinimumSize(new java.awt.Dimension(420, 160));
        dialogoCancelarPago.setModal(true);
        dialogoCancelarPago.setResizable(false);

        botonSiEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/Img/Iconos16X16/Aceptar.png"))); // NOI18N
        botonSiEliminar.setText("Si");
        botonSiEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonSiEliminarActionPerformed(evt);
            }
        });

        botonNoEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/Img/Iconos16X16/Cancelar.png"))); // NOI18N
        botonNoEliminar.setText("No");
        botonNoEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonNoEliminarActionPerformed(evt);
            }
        });

        etiquetaEliminar.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        etiquetaEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/Img/Iconos35X35/Papelera.png"))); // NOI18N
        etiquetaEliminar.setText("¿Confirma que desea cancelar el pago seleccionado?");

        javax.swing.GroupLayout dialogoCancelarPagoLayout = new javax.swing.GroupLayout(dialogoCancelarPago.getContentPane());
        dialogoCancelarPago.getContentPane().setLayout(dialogoCancelarPagoLayout);
        dialogoCancelarPagoLayout.setHorizontalGroup(
            dialogoCancelarPagoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, dialogoCancelarPagoLayout.createSequentialGroup()
                .addContainerGap(248, Short.MAX_VALUE)
                .addComponent(botonSiEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(botonNoEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addComponent(etiquetaEliminar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        dialogoCancelarPagoLayout.setVerticalGroup(
            dialogoCancelarPagoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(dialogoCancelarPagoLayout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addComponent(etiquetaEliminar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
                .addGroup(dialogoCancelarPagoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonSiEliminar)
                    .addComponent(botonNoEliminar))
                .addContainerGap())
        );

        setPreferredSize(new java.awt.Dimension(953, 603));

        jPanel5.setBackground(new java.awt.Color(255, 255, 255));
        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1), "Pagos con Bono", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Tahoma", 0, 36))); // NOI18N

        jLabel4.setText("Buscar ");

        tablaPagos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null}
            },
            new String [] {
                "Id", "Rut profesional", "Nombre profesional", "Rut paciente", "Nombre paciente", "N° Bono", "Fecha", "Hora", "Valor total"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.Object.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, true, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tablaPagos.getTableHeader().setReorderingAllowed(false);
        tablaPagos.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tablaPagosMouseClicked(evt);
            }
        });
        jScrollPane4.setViewportView(tablaPagos);

        campoBuscar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                campoBuscarKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap(728, Short.MAX_VALUE)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(campoBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addComponent(jScrollPane4, javax.swing.GroupLayout.Alignment.TRAILING)
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(campoBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 409, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel6.setBackground(new java.awt.Color(255, 255, 255));

        etiquetaMensajeria.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        etiquetaMensajeria.setText("Área de mensajería");

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(etiquetaMensajeria, javax.swing.GroupLayout.DEFAULT_SIZE, 953, Short.MAX_VALUE)
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(etiquetaMensajeria, javax.swing.GroupLayout.DEFAULT_SIZE, 27, Short.MAX_VALUE)
        );

        jPanel7.setBackground(new java.awt.Color(255, 255, 255));

        botonVer.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/Img/Iconos35X35/Previsualizar.png"))); // NOI18N
        botonVer.setText("Ver");
        botonVer.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        botonVer.setMargin(new java.awt.Insets(2, 4, 2, 6));
        botonVer.setMaximumSize(new java.awt.Dimension(97, 43));
        botonVer.setMinimumSize(new java.awt.Dimension(97, 43));
        botonVer.setPreferredSize(new java.awt.Dimension(97, 43));
        botonVer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonVerActionPerformed(evt);
            }
        });

        botonModificar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/Img/Iconos35X35/Edit.png"))); // NOI18N
        botonModificar.setText("Modificar");
        botonModificar.setMargin(new java.awt.Insets(2, 4, 2, 6));
        botonModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonModificarActionPerformed(evt);
            }
        });

        botonEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/Img/Iconos35X35/delete_32x32x32.png"))); // NOI18N
        botonEliminar.setText("Eliminar");
        botonEliminar.setMargin(new java.awt.Insets(2, 4, 2, 6));
        botonEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonEliminarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel7Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(botonModificar, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(botonEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(botonVer, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jPanel7Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {botonEliminar, botonModificar, botonVer});

        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addGap(13, 13, 13)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonEliminar)
                    .addComponent(botonVer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(botonModificar))
                .addContainerGap())
        );

        jPanel7Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {botonEliminar, botonModificar, botonVer});

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents

    public void filtrarTabla(java.util.List<service.PagoBono> pagos) {
        this.limpiarTabla();
        int i = 0;
        for (service.PagoBono pago : pagos) {
            if (pago.isVisible()) {
                this.agregarFila();
                tablaPagos.setValueAt(pago.getId().toString(), i, 0);
                tablaPagos.setValueAt(pago.getRutProfesional(), i, 1);
                tablaPagos.setValueAt(pago.getNombreProfesional(), i, 2);                
                tablaPagos.setValueAt(pago.getRutPaciente(), i, 3);
                tablaPagos.setValueAt(pago.getNombrePaciente(), i, 4);
                tablaPagos.setValueAt(pago.getNumeroBono(), i, 5);
                tablaPagos.setValueAt(pago.getFecha(), i, 6);
                tablaPagos.setValueAt(pago.getHora(), i, 7);
                tablaPagos.setValueAt(pago.getValorTotal(), i, 8);
                i++;
            }
        }
    }
    
    public void actualizarTabla() {
        this.limpiarTabla();
        java.util.List<service.PagoBono> pagos = listarPagoBono();
        int i = 0;
        for (service.PagoBono pago : pagos) {
            if (pago.isVisible()) {
                this.agregarFila();
                tablaPagos.setValueAt(pago.getId().toString(), i, 0);
                tablaPagos.setValueAt(pago.getRutProfesional(), i, 1);
                tablaPagos.setValueAt(pago.getNombreProfesional(), i, 2);                
                tablaPagos.setValueAt(pago.getRutPaciente(), i, 3);
                tablaPagos.setValueAt(pago.getNombrePaciente(), i, 4);
                tablaPagos.setValueAt(pago.getNumeroBono(), i, 5);
                tablaPagos.setValueAt(pago.getFecha(), i, 6);
                tablaPagos.setValueAt(pago.getHora(), i, 7);
                tablaPagos.setValueAt(pago.getValorTotal(), i, 8);
                i++;
            }
        }
    }

    public void agregarFila() {
        DefaultTableModel temp = (DefaultTableModel) tablaPagos.getModel();
        Object nuevo[] = {"", "", "", "", "", "", "", "", ""};
        temp.addRow(nuevo);
    }

    public void cambiarEtiquetaMensajeria(String mensaje, Color color) {
        etiquetaMensajeria.setText(mensaje);
        etiquetaMensajeria.setForeground(color);
    }

    public void limpiarTabla() {
        DefaultTableModel temp = (DefaultTableModel) tablaPagos.getModel();
        for (int i = tablaPagos.getRowCount() - 1; i >= 0; i--) {
            temp.removeRow(i);
        }
    }

    public void normalizarEtiquetaMensajeria() {
        etiquetaMensajeria.setText("Área de mensajería");
        etiquetaMensajeria.setForeground(Color.black);
    }

    public void ocultarColumnaID() {
        tablaPagos.getColumnModel().getColumn(0).setMaxWidth(0);
        tablaPagos.getColumnModel().getColumn(0).setMinWidth(0);
        tablaPagos.getColumnModel().getColumn(0).setPreferredWidth(0);
    }
    
    private void botonModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonModificarActionPerformed
        service.Privilegios p = (service.Privilegios) cargarPrivilegios(Login.getUser().getRut());
        if (p.isModificarPagosConBono()) {
            this.normalizarEtiquetaMensajeria();
            if (tablaPagos.getSelectedRow() == -1) {
                this.cambiarEtiquetaMensajeria("Debe seleccionar un pago para ser modificado", Color.red);
            } else {
                DialogoInfoPagoBono dip = new DialogoInfoPagoBono(null, true, this, Long.parseLong((String) tablaPagos.getValueAt(tablaPagos.getSelectedRow(), 0)));
                dip.normalizarMensajeria();
                dip.setLocationRelativeTo(null);
                dip.setTitle("Modificar pago con bono");
                dip.getBotonGuardar().setText("Guardar");
                dip.getBotonGuardar().setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/Img/Iconos16X16/Aceptar.png")));
                service.PagoBono pag = (service.PagoBono) cargarPagoBono((Long.parseLong((String) tablaPagos.getValueAt(tablaPagos.getSelectedRow(), 0))));
                dip.llenarCampos(pag);
                dip.habilitarCampos();
                dip.setVisible(true);
            }
        } else {
            JOptionPane.showMessageDialog(this, "No tiene permisos para esta acción, \n contacte al administrador para más información");
        }
    }//GEN-LAST:event_botonModificarActionPerformed

    private void botonEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonEliminarActionPerformed
        service.Privilegios p = (service.Privilegios) cargarPrivilegios(Login.getUser().getRut());
        if (p.isCancelarPagosConBono()) {
            this.normalizarEtiquetaMensajeria();
            int cont = 0;
            for (int i = 0; i < tablaPagos.getRowCount(); i++) {
                if (tablaPagos.isRowSelected(i)) {
                    cont++;
                }
            }
            if (cont >= 1) {
                if (cont > 1) {
                    etiquetaEliminar.setText("¿Confirma que desea eliminar los pagos seleccionados?");
                } else if (cont == 1 || tablaPagos.getSelectedRow() != -1) {
                    etiquetaEliminar.setText("¿Confirma que desea eliminar el pago seleccionado?");
                }
                dialogoCancelarPago.setLocationRelativeTo(null);
                dialogoCancelarPago.setVisible(true);
            } else {
                this.cambiarEtiquetaMensajeria("Debe seleccionar un pago para ser eliminado", Color.red);
            }
        } else {
            JOptionPane.showMessageDialog(this, "No tiene permisos para esta acción, \n contacte al administrador para más información");
        }
    }//GEN-LAST:event_botonEliminarActionPerformed

    private void botonVerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonVerActionPerformed
        service.Privilegios p = (service.Privilegios) cargarPrivilegios(Login.getUser().getRut());
        if (p.isVerPagosConBono()) {
            this.normalizarEtiquetaMensajeria();
            if (tablaPagos.getSelectedRow() == -1) {
                this.cambiarEtiquetaMensajeria("Debe seleccionar un pago para verlo", Color.red);
            } else {
                DialogoInfoPagoBono dip = new DialogoInfoPagoBono(null, true, this, Long.parseLong((String) tablaPagos.getValueAt(tablaPagos.getSelectedRow(), 0)));
                dip.normalizarMensajeria();
                dip.setLocationRelativeTo(null);
                dip.setTitle("Ver pago con bono");
                dip.getBotonGuardar().setText("Exportar");
                dip.getBotonGuardar().setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/Img/Iconos16X16/pdf.png")));
                service.PagoBono pres = (service.PagoBono) cargarPagoBono((Long.parseLong((String) tablaPagos.getValueAt(tablaPagos.getSelectedRow(), 0))));
                dip.llenarCampos(pres);
                dip.inhabilitarCampos();
                dip.setVisible(true);
            }
        } else {
            JOptionPane.showMessageDialog(this, "No tiene permisos para esta acción, \n contacte al administrador para más información");
        }
    }//GEN-LAST:event_botonVerActionPerformed

    private void botonSiEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonSiEliminarActionPerformed
        dialogoCancelarPago.dispose();
        for (int i = 0; i < tablaPagos.getRowCount(); i++) {
            if (tablaPagos.isRowSelected(i)) {
                Long id = Long.parseLong((String) tablaPagos.getValueAt(i, 0));
                String valorPagado = tablaPagos.getValueAt(i, 8).toString();
                this.cancelarPago(id, valorPagado);
                service.PagoBono spb=(service.PagoBono) cargarPagoBono(id);
                String valorNuevo = "Número bono: " + spb.getNumeroBono() + "\n" + "Rut profesional: " + spb.getRutProfesional() + "\n" + "Nombre profesional: " + spb.getNombreProfesional()
                        + "\n" + "Rut paciente: " + spb.getRutPaciente() + "\n" + "Nombre paciente: " + spb.getNombrePaciente()
                        + "\n" + "Fecha: " + spb.getFecha() + "\n" + "Hora: " + spb.getHora() + "\n" + "Número de sesiones: " + spb.getNroSesiones() + "\n" + "Valor total: " + spb.getValorTotal();
                registrar(Login.getUser(), "Pago con bono", "Eliminó con número de bono: " + spb.getNumeroBono(), "No aplica", valorNuevo);
                eliminarPagoBono(id);
            }
        }
        this.actualizarTabla();
    }//GEN-LAST:event_botonSiEliminarActionPerformed

    private void botonNoEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonNoEliminarActionPerformed
        dialogoCancelarPago.dispose();
    }//GEN-LAST:event_botonNoEliminarActionPerformed

    private void campoBuscarKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_campoBuscarKeyReleased
        if (!campoBuscar.getText().equals("")) {
            limpiarTabla();
            java.util.List<service.PagoBono> lista = (java.util.List<service.PagoBono>) filtrarPagoBono(campoBuscar.getText());
            filtrarTabla(lista);
        } else {
            limpiarTabla();
            actualizarTabla();
        }
    }//GEN-LAST:event_campoBuscarKeyReleased

    private void tablaPagosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaPagosMouseClicked
        int fila = tablaPagos.rowAtPoint(evt.getPoint());
        if ((fila > -1) && evt.getClickCount() == 2) {
            botonVerActionPerformed(null);
        }
    }//GEN-LAST:event_tablaPagosMouseClicked

    public void cancelarPago(Long id, String valorPagado) {
        service.PagoBono pe = (service.PagoBono) cargarPagoBono(id);
        service.Cita cita = (service.Cita) buscarCitaPorId(pe.getIdCita());
        if (cita != null) {
            cita.setEstado("Confirmada");
            modificarCita(cita);
            int valorPag = Integer.parseInt(valorPagado.replace("$", "").replace(".", ""));
            if(cita.getTipoPrograma().equalsIgnoreCase("FONASA")){
                service.ProgramaMedicoFonasa pmf = (service.ProgramaMedicoFonasa)cargarProgramaFonasaPorNumero(cita.getIdPrograma());
                pmf.setPagado(false);
                int valorProg = Integer.parseInt(pmf.getValorTotal().replace("$", "").replace(".", ""));
                int valorNuevo = valorProg + valorPag;
                pmf.setValorTotal("$"+df.format(valorNuevo));
                modificarProgramaFonasa(pmf);                
            }
            else if(cita.getTipoPrograma().equalsIgnoreCase("DIPRECA")){
                service.ProgramaDipreca pd = (service.ProgramaDipreca)obtenerProgramaDipreca(cita.getIdPrograma());
                pd.setPagado(false);
                int valorProg = Integer.parseInt(pd.getValorTotal().replace("$", "").replace(".", ""));
                int valorNuevo = valorProg + valorPag;
                pd.setValorTotal("$"+df.format(valorNuevo));
                editarProgramaDipreca(pd);               
            }
            else if(cita.getTipoPrograma().equalsIgnoreCase("JEAFOSALE")){
                service.ProgramaMedicoJeafosale pd = (service.ProgramaMedicoJeafosale)obtenerProgramaJeafosale(cita.getIdPrograma());
                pd.setPagado(false);
                int valorProg = Integer.parseInt(pd.getValorTotal().replace("$", "").replace(".", ""));
                int valorNuevo = valorProg + valorPag;
                pd.setValorTotal("$"+df.format(valorNuevo));
                editarProgramaJeafosale(pd);               
            }
        }        
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonEliminar;
    private javax.swing.JButton botonModificar;
    private javax.swing.JButton botonNoEliminar;
    private javax.swing.JButton botonSiEliminar;
    private javax.swing.JButton botonVer;
    private javax.swing.JTextField campoBuscar;
    private javax.swing.JDialog dialogoCancelarPago;
    private javax.swing.JLabel etiquetaEliminar;
    private javax.swing.JLabel etiquetaMensajeria;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTable tablaPagos;
    // End of variables declaration//GEN-END:variables

    private static java.util.List<service.PagoBono> listarPagoBono() {
        service.PagoWS_Service service = new service.PagoWS_Service();
        service.PagoWS port = service.getPagoWSPort();
        return port.listarPagoBono();
    }

    private static Object buscarCitaPorId(java.lang.Long id) {
        service.AgendaWS_Service service = new service.AgendaWS_Service();
        service.AgendaWS port = service.getAgendaWSPort();
        return port.buscarCitaPorId(id);
    }

    private static Object cargarPagoBono(java.lang.Long id) {
        service.PagoWS_Service service = new service.PagoWS_Service();
        service.PagoWS port = service.getPagoWSPort();
        return port.cargarPagoBono(id);
    }

    private static boolean eliminarPagoBono(java.lang.Long id) {
        service.PagoWS_Service service = new service.PagoWS_Service();
        service.PagoWS port = service.getPagoWSPort();
        return port.eliminarPagoBono(id);
    }

    private static boolean modificarCita(service.Cita arg0) {
        service.AgendaWS_Service service = new service.AgendaWS_Service();
        service.AgendaWS port = service.getAgendaWSPort();
        return port.modificarCita(arg0);
    }

    private static Object cargarPrivilegios(java.lang.String rut) {
        service.PrivilegiosWS_Service service = new service.PrivilegiosWS_Service();
        service.PrivilegiosWS port = service.getPrivilegiosWSPort();
        return port.cargarPrivilegios(rut);
    }

    private static Long registrar(service.Usuario usuario, java.lang.String modulo, java.lang.String accion, java.lang.String valorAnterior, java.lang.String valorNuevo) {
        service.BitacoraWSService service = new service.BitacoraWSService();
        service.BitacoraWS port = service.getBitacoraWSPort();
        return port.registrar(usuario, modulo, accion, valorAnterior, valorNuevo);
    }

    private static java.util.List<service.PagoBono> filtrarPagoBono(java.lang.String buscar) {
        service.PagoWS_Service service = new service.PagoWS_Service();
        service.PagoWS port = service.getPagoWSPort();
        return port.filtrarPagoBono(buscar);
    }

    //DIPRECA
    private static Object obtenerProgramaDipreca(java.lang.String numero) {
        service.ProgramaDiprecaWSService service = new service.ProgramaDiprecaWSService();
        service.ProgramaDiprecaWS port = service.getProgramaDiprecaWSPort();
        return port.obtenerProgramaDipreca(numero);
    }

    private static boolean editarProgramaDipreca(service.ProgramaDipreca programaDipreca) {
        service.ProgramaDiprecaWSService service = new service.ProgramaDiprecaWSService();
        service.ProgramaDiprecaWS port = service.getProgramaDiprecaWSPort();
        return port.editarProgramaDipreca(programaDipreca);
    }   
    
    //JEAFOSALE

    private static Object obtenerProgramaJeafosale(java.lang.String numero) {
        service.ProgramaJeafosaleWSService service = new service.ProgramaJeafosaleWSService();
        service.ProgramaJeafosaleWS port = service.getProgramaJeafosaleWSPort();
        return port.obtenerProgramaJeafosale(numero);
    }

    private static boolean editarProgramaJeafosale(service.ProgramaMedicoJeafosale programaDipreca) {
        service.ProgramaJeafosaleWSService service = new service.ProgramaJeafosaleWSService();
        service.ProgramaJeafosaleWS port = service.getProgramaJeafosaleWSPort();
        return port.editarProgramaJeafosale(programaDipreca);
    }
    
    //FONASA

    private static Object cargarProgramaFonasaPorNumero(java.lang.String id) {
        service.ProgramaFonasaWSService service = new service.ProgramaFonasaWSService();
        service.ProgramaFonasaWS port = service.getProgramaFonasaWSPort();
        return port.cargarProgramaFonasaPorNumero(id);
    }

    private static boolean modificarProgramaFonasa(service.ProgramaMedicoFonasa programaMedico) {
        service.ProgramaFonasaWSService service = new service.ProgramaFonasaWSService();
        service.ProgramaFonasaWS port = service.getProgramaFonasaWSPort();
        return port.modificarProgramaFonasa(programaMedico);
    } 
    
}
